package com.qujia.station.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.AppUserSuper;
import com.qujia.station.domain.Enterprise;
import com.qujia.station.service.IAppUserSuperService;
import com.qujia.station.service.IEnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 【超级会员】Controller
 * 
 * @author qujia
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/appusersuper")
@Api(tags = "超级会员")
public class AppUserSuperController extends BaseController
{
    @Autowired
    private IAppUserSuperService appUserSuperService;
    @Resource
    private IEnterpriseService enterpriseService;
    /**
     * 查询【超级会员】列表
     */
    @GetMapping("/list")
    @ApiOperation("获取超级会员列表")
    public TableDataInfo list(AppUserSuper appUserSuper)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if (currentUser != null) {
            SysUser sysUser = currentUser.getUser();
            if (StringUtils.equalsAny(sysUser.getUserLevel(),UserConstants.LEVEL_TWO,UserConstants.LEVEL_THREE)) {
                // 不是admin设置本油企id
                appUserSuper.setEnterpriseId(sysUser.getEnterpriseId());
            }
        }

        SysUser user = currentUser.getUser();
        if ("7".equals(user.getUserLevel())){
            List<Enterprise> enterpriseList =
                    enterpriseService.list(new QueryWrapper<Enterprise>().in("author_id", user.getUserId()));
            if (enterpriseList != null && enterpriseList.size()>0) {
                List<Long> enterpriseIds = enterpriseList.stream().map(Enterprise::getEnterpriseId).collect(Collectors.toList());
                appUserSuper.setEnterpriseIdListForAuthor(enterpriseIds);

            } else {
                throw new CustomException(UserConstants.NO_ENTERPRISE);
            }
        }

        startPage();
        List<AppUserSuper> list = appUserSuperService.selectAppUserSuperList(appUserSuper);
        return getDataTable(list);
    }

    /**
     * 导出【超级会员】列表
     */
    @Log(title = "【超级会员】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppUserSuper appUserSuper)
    {
        List<AppUserSuper> list = appUserSuperService.selectAppUserSuperList(appUserSuper);
        ExcelUtil<AppUserSuper> util = new ExcelUtil<AppUserSuper>(AppUserSuper.class);
        return util.exportExcel(list, "super");
    }

    /**
     * 获取【超级会员】详细信息
     */
    @GetMapping(value = "/{superId}")
    @ApiOperation("获取超级会员详情")
    public AjaxResult getInfo(@PathVariable("superId") Long superId)
    {
        return AjaxResult.success(appUserSuperService.selectAppUserSuperById(superId));
    }

    /**
     * 新增【超级会员】
     */
    @Log(title = "【超级会员】", businessType = BusinessType.INSERT)
    @ApiOperation("添加超级会员")
    @PostMapping
    public AjaxResult add(@RequestBody AppUserSuper appUserSuper)
    {
        return toAjax(appUserSuperService.insertAppUserSuper(appUserSuper));
    }

    @Log(title = "【超级会员】", businessType = BusinessType.INSERT)
    @ApiOperation("超级会员和手机号绑定")
    @PostMapping(value = "/addphone")
    public AjaxResult addPhone(@RequestBody AppUserSuper appUserSuper)
    {
        return AjaxResult.success(appUserSuperService.insertAppUserSuperPhone(appUserSuper));
    }
    /**
     * 修改【超级会员】
     */
    @Log(title = "【超级会员】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppUserSuper appUserSuper)
    {
        return toAjax(appUserSuperService.updateAppUserSuper(appUserSuper));
    }

    /**
     * 删除【超级会员】
     */
    @Log(title = "【超级会员】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{superIds}")
    public AjaxResult remove(@PathVariable Long[] superIds)
    {
        return toAjax(appUserSuperService.deleteAppUserSuperByIds(superIds));
    }
}
